package com.jiaming.restaurant.api.goods.controller;

import cn.hutool.core.util.StrUtil;
import com.jiaming.restaurant.api.common.bean.contants.CommonContants;
import com.jiaming.restaurant.api.common.bean.vo.MyPageVO;
import com.jiaming.restaurant.api.common.response.ResultCode;
import com.jiaming.restaurant.api.goods.bean.entity.Goods;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsDisableVO;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsUpdateVO;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsVO;
import com.jiaming.restaurant.api.goods.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dragon
 * @create 2020-12-28 16:02
 */
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody Goods goods) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验
//        String name = goods.getName();
//        if (StrUtil.isEmpty(name) || name.length() < 6 || name.length() > 40) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer disable = goods.getDisable();
//        if (disable != null && (disable < 0 || disable > 1)) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer price = goods.getPrice();
//        if (price == null || price <= 100) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = goodsService.save(goods);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        result.put("data", goods.getId());
        return result;
    }

    @PostMapping(value = "/update")
    public Object update(@RequestBody GoodsUpdateVO goodsVO) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验
        String name = goodsVO.getName();
        if (StrUtil.isNotEmpty(name) && (name.length() < 6 || name.length() > 40)) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        Integer id = goodsVO.getId();
        if (id == null || id <= 0) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        Integer price = goodsVO.getPrice();
        if (price != null && price <= 100) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        Goods goods = new Goods();
        goods.setId(id);
        goods.setName(goods.getName());
        goods.setPrice(goods.getPrice());
        boolean isOk = goodsService.updateById(goods);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/disable")
    public Object disable(@RequestBody GoodsDisableVO disableVO) {
        Map<String, Object> result = new HashMap<>();
        List<Integer> ids = disableVO.getIds();
        if (ids == null || ids.size() == 0) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        Integer disable = disableVO.getDisable();
        if (disable == null || (disable < 0 || disable > 1)) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        boolean isOk = goodsService.disable(disableVO);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/get")
    public Object get(@RequestBody GoodsVO goods) {
        Map<String, Object> result = new HashMap<>();
        Integer id = goods.getId();
        if (id == null || id <= 0) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        Goods data = goodsService.getById(id);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", data);
        return result;
    }

    @PostMapping(value = "/page")
    public Object page(@RequestBody GoodsVO goods) {
        Integer pageNum = goods.getPageNum();
        if (pageNum == null || pageNum <= 0) {
            goods.setPageNum(1);
        }
        Long pageSize = goods.getPageSize();
        if (pageSize == null || pageSize <= CommonContants.MIN_PAGE_SIZE) {
            goods.setPageSize(CommonContants.MIN_PAGE_SIZE);
        }
        MyPageVO<Goods> data = goodsService.page(goods);
        Map<String, Object> result = new HashMap<>();
        result.put("code", ResultCode.SUCCESS);
        result.put("data", data);
        return result;
    }

    @PostMapping(value = "deleteById")
    public Object delete(@RequestParam Integer id) {
        boolean byId = goodsService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", ResultCode.SUCCESS);
        result.put("data", byId);
        return result;
    }

    @PostMapping(value = "getById")
    public Object queryById(@RequestParam Integer id) {
        Goods byId = goodsService.getById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", ResultCode.SUCCESS);
        result.put("data", byId);
        return result;
    }
}
